import {t, api, message, bs} from "@/service/index.js"


export default {
    url: {
        login: "/user/login",
        enroll: "/user/enroll",
    },
    enroll(data) {
        return api.post(this.url.enroll, data)
    },
    async login(data, router) {
        try {
            let result = await api.post(this.url.login, {
                account: data.account,
                password: data.password,
            }).then(res => {
                bs().token = res.data.token
                // 得到两个路由，一个前端所有路由，一个后端所有路由，以后端路由为准，不是tree结构
                bs().formatRouter(res.data.role.menus, router)
                message.success(t('login.success'))
                return true
            }).catch(e => {
                return false
            })
            // 把tree结构路由转扁平化
            // let frontRouter = interaction.treeToFlat(ro,'children')
            return result
        } catch (e) {
            message.error(t('login.fail'))
            console.log(e)
        }
    },
}